{% extends "article/base.html" %}

{% load staticfiles %}

{% block title %}
    article
{% endblock %}

{% block content %}
    <link rel="stylesheet" href="{% static 'css/editormd.css' %}">
    <link rel="stylesheet" href="{% static 'css/style.css' %}">
    <div class="col-md-10">
        <div style="margin-left: 10px">
            <form action="." method="post" class="form-horizontal">{% csrf_token %}
                <div class="row" style="margin-top:10px">
                    <div class="col-md-2 text-right"><span>标题:</span></div>
                    <div class="col-md-10 text-left">
                        {{ this_article_form.title }}
                    </div>
                </div>
                <div class="row" style="margin-top: 10px">
                    <div class="col-md-2 text-right"><span>栏目:</span></div>
                    <div class="col-md-10 text-left">
                        <select name="" id="which_column">
                            {% for article_column in article_columns %}
                                {% if article_column == this_article_column %}
                                    <option value="{{ article_column.id }}"
                                            selected="selected">{{ article_column.column }}</option>
                                {% else %}
                                    <option value="{{ article_column.id }}">{{ article_column.column }}</option>
                                {% endif %}
                            {% endfor %}

                        </select>
                    </div>
                </div>
                <div class="row" style="margin-top:10px">
                    <div class="col-md-2 text-right"><span>内容:</span></div>
                    <div id="editormd" class="col-md-10 text-left">
                        <textarea name="" id="id_body" style="display: none;">
                            {{ article.body }}
                        </textarea>
                    </div>
                </div>
                <div class="row">
                    <input type="button" class="btn btn-primary btn-lg" value="发布" onclick="redit_article()">
                </div>

            </form>
        </div>
    </div>
    <script src="{% static 'js/jquery.min.js' %}"></script>
    <script src="{% static 'editormd.min.js' %}"></script>
    <script src="{% static 'js/layer.js' %}"></script>
    <script type="text/javascript">
    $(function () {
        var editor = editormd("editormd", {
            width:"100%",
            height:640,
            path:"{% static 'lib/' %}"
        });
    });

    function redit_article() {
        var title = $("#id_title").val();
        var column_id = $("#which_column").val();
        var body = $("#id_body").val();
        $.ajax({
            url:"{% url 'article:redit_article' article.id %}",
            type:"POST",
            data:{"title":title, "body":body, "column_id":column_id},
            success:function (e) {
                if(e == "1"){
                    layer.msg("编辑成功");
                    location.href = "{% url 'article:article_list' %}";
                }else{
                    layer.msg("编辑失败");
                }
            }
        })

    }
    </script>
{% endblock %}